<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="50" align="center"></el-table-column>
      <el-table-column prop="avatar" label="头像" width="80">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="50" :src=scope.row.avatar></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100" align="center"></el-table-column>
      <el-table-column prop="phone" label="电话" width="150"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="200" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" align="center"></el-table-column>
      <el-table-column label="是否启用" width="80" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="toggleEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button type="primary" icon="el-icon-edit" circle size="medium" @click="getListItem(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="medium" @click="del(scope.row)"></el-button>
          </el-row>
          <el-dialog title="修改管理员" :visible.sync="dialogFormVisible">
            <el-form :model="form">
              <el-form-item label="管理员id" :label-width="formLabelWidth">
                <el-input v-model="form.id" autocomplete="off" disabled></el-input>
              </el-form-item>
              <el-form-item label="头像" :label-width="formLabelWidth">
                <el-input v-model="form.avatar" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="管理员姓名" :label-width="formLabelWidth">
                <el-input v-model="form.username" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="管理员密码" :label-width="formLabelWidth">
                <el-input v-model="form.password" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="管理员昵称" :label-width="formLabelWidth">
                <el-input v-model="form.nickname" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="电话号码" :label-width="formLabelWidth">
                <el-input v-model="form.phone" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="电子邮箱" :label-width="formLabelWidth">
                <el-input v-model="form.email" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="简介" :label-width="formLabelWidth">
                <el-input v-model="form.description" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="submit" @click="submit()">确 定</el-button>
            </div>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script> import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        id: '',
        avatar: '',
        username: '',
        password: '',
        nickname: '',
        phone: '',
        email: '',
        description: '',
        enable: '',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    loadAdminList(){
      let url = "http://localhost:9080/admins/list"
      console.log("url:"+url)
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if(responseBody.state == 20000){
          this.tableData = responseBody.data;
        }
      })
    },
    getListItem(admin){
      this.dialogFormVisible = true;
      this.form = admin;
    },
    submit() {
      let url = 'http://localhost:9080/admins/'+this.form.id+'/update'
      let formData = this.qs.stringify(this.form);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.$message.success("修改成功");
          this.dialogFormVisible = false;
          this.loadAdminList();
        } else if (responseBody.state == 40400 ){
          this.$alert(responseBody.message, '警告',{
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = true;
              this.loadAdminList();
            }
          });
        } else {
          this.$alert(responseBody.message, '警告',{
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = true;
              this.loadAdminList();
            }
          });
        }
      })
    },
    del(admin) {
      this.$confirm('此操作将永久删除'+'id为'+admin.id+'的数据, 是否确认?', 'Warning', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(admin);
      }).catch(() => {
        this.$message.info("已取消");
      });
    },
    handleDelete(admin) {
      let url = 'http://localhost:9080/admins/'+admin.id+'/delete'
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if(responseBody.state == 20000){
          this.$message.success("删除成功！");
          this.loadAdminList();
        }else if(responseBody.state==40400){
          this.$message.warning(responseBody.message);
        }else {
          this.$message.warning(responseBody.message);
        }
      })
    },
    toggleEnable(admin){
      let enableText = ['禁用', '启用'];
      let url = 'http://localhost:9080/admins/'+admin.id+'/';
      if(admin.enable==1){
        url+='enable';
      } else {
        url+='disable'
      }
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state==20000){
          this.$message.success('成功将【'+admin.username+'】设置为【'+enableText[admin.enable]+'】状态');
        }else{
          this.$message.error(responseBody.message);
        }
      })
    },
  },
  mounted() {
    this.loadAdminList();
  }
} </script>
